package com.novellotus.iov.carvice.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.novellotus.httploadingview.LoadingView;
import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.adapter.MessageAdapter;
import com.novellotus.iov.carvice.bean.RxMessageBean;
import com.novellotus.iov.carvice.retrofit.BaseEntity;
import com.novellotus.iov.carvice.retrofit.BaseObserver;
import com.novellotus.iov.carvice.retrofit.RetrofitFactory;
import com.novellotus.iov.carvice.retrofit.RxSchedulers;
import com.novellotus.iov.carvice.utils.MyUtils;
import com.novellotus.iov.carvice.utils.MyViewUtils;
import com.novellotus.iov.carvice.utils.SharedPreferencesUtils;
import com.novellotus.iov.carvice.view.PageView;
import com.orangegangsters.github.swipyrefreshlayout.library.RefreshLayout;
import com.orangegangsters.github.swipyrefreshlayout.library.RefreshLayoutDirection;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;

/**
 * A simple {@link Fragment} subclass.
 */
public class NewsFragment extends BaseFragment implements RefreshLayout.OnRefreshListener, LoadingView.OnBtnClickListener, PageView.PageViewInterface {

    @BindView(R.id.pageView)
    PageView pageView;
    MessageAdapter messageAdapter;
    final int MAX_PAGE = 10;

    public NewsFragment() {
        // Required empty public constructor
    }


    @Override
    View initRootView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_message, container, false);
    }

    @Override
    void init() {
        pageView.setLoadFinish(false);
        pageView.initLoadingView(this);
        pageView.refreshLayout.setOnRefreshListener(this);
        pageView.setPageViewInterface(this);
        pageView.loadingView.mWarnView.setTextColor(getResources().getColor(R.color.text_black));
        pageView.setReLoadBtnListener(this);
        getMessageData(1);
    }

    /**
     * 获取消息列表
     */
    Observable<BaseEntity<RxMessageBean>> observable;

    public void getMessageData(int currentPate) {
        observable = RetrofitFactory.getInstance().getMessage(SharedPreferencesUtils.getKey(getContext())[0], currentPate, MAX_PAGE);
        observable.compose(RxSchedulers.<BaseEntity<RxMessageBean>>compose()).subscribe(new BaseObserver<RxMessageBean>(getContext()) {
            @Override
            public void onSuccess(RxMessageBean response) {
                if (response.getTotal() == 0) {
                    pageView.loadFail("暂无消息", false);
                } else {
                    pageView.loadSuccess();
                    pageView.setTotalPage(response.getTotal(), MAX_PAGE);
                    if (messageAdapter != null) {
                        if (response.getCurrent() == 1) {
                            messageAdapter.update(response.getRecords());
                        } else {
                            messageAdapter.addData(response.getRecords());
                        }
                    } else {
                        setData(response.getRecords());
                    }
                }
            }

            @Override
            public void onFail(int code, String msg) {
                pageView.loadFail(msg, true);
            }

            @Override
            public void onError(String msg) {
                pageView.loadFail(msg, true);
            }
        });
    }

    public void setData(List<RxMessageBean.RecordsBean> recordsBeanList) {
        messageAdapter = new MessageAdapter(recordsBeanList);
        pageView.setBottomInterface(messageAdapter);
        pageView.setRecyclerViewAdapter(MyViewUtils.getLinearLayoutManager(getContext()), messageAdapter);
    }

    @Override
    public void loadData(int dataPage) {
        getMessageData(pageView.currentPage);
    }

    @Override
    public void onReload() {
        getMessageData(1);
    }

    @Override
    public void onRefresh(RefreshLayoutDirection direction) {
        pageView.refreshData(direction);
    }

}
